import { getCurrentState } from '../../store'
import {
  receiveMessage,
  groupMessageResponse
} from '@/utils/messageUtil'

/**
 * 消息类型控制器, 负责接收 响应消息, 并转发给对应的处理函数
 * @param type 消息类型
 * @param data 响应数据
 */
function dispatcherServlet(data) {
  // let obj = JSON.parse(data);
  // eval(obj.type + ('obj'))
  // eslint-disable-next-line no-eval
  eval(data.type + '(data)')
}

/**
 * 认证
 * @param data
 * @constructor
 */
// eslint-disable-next-line no-unused-vars
function AUTHORIZATION(data) {
  const message = dataFormat(data)
}

/**
 * 私聊
 * @param data
 * @constructor
 */
// eslint-disable-next-line no-unused-vars
function CHAT_REDIRECT(data) {
  const message = dataFormat(data)
  receiveMessage(message, getCurrentState())
}

/**
 * 群聊
 * @param data
 * @constructor
 */
// eslint-disable-next-line no-unused-vars
function CHAT_REDIRECT_TO_GROUP_REQUEST(data) {
  const message = dataFormat(data)
  groupMessageResponse(message, getCurrentState())
}

function dataFormat(data) {
  // let obj = JSON.parse(data);
  const message = JSON.parse(data.message)
  return message
}

export { dispatcherServlet }
